import time

import arrow
from flask import Blueprint, request, redirect, make_response, Response, jsonify, render_template
from application.models.model import User, Role, role_user, BrokerUser, News, Course
from db_init import db
from swaager_init import siwa, BaseModel
from sqlalchemy import text
from flask_login import current_user, login_required

from tools.data_tools import orm2dict, ret_route, limit_offset

index_blue = siwa.blueprint("index", __name__)


@index_blue.route("/", methods=['get'])
def index_search():

    search = request.args.get('search')
    course_data = Course.query.filter(Course.name.like(f"%{search}%")).all()
    news_data = News.query.filter(News.title.like(f"%{search}%")).all()
    ret_data = {
        'course': orm2dict(course_data),
        'news': orm2dict(news_data),
    }
    return ret_route(data=ret_data)
